﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Eugenij
 * Дата: 30.01.2014
 * Время: 21:48
 * 
 * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
 */
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace hwspeed
{
  
    /// <summary>
    /// Description of Configuration.
    /// </summary>
    /// 
    
    public sealed class Configuration
    {
        public struct DCB
        {
        	public long DCBlength;
        	public long BaudRate;
        	public long fBitFields;
        	public int wReserved;
        	public int XonLim;
        	public int XoffLim;
        	public byte ByteSize;
        	public byte Parity;
        	public byte StopBits;
        	public byte XonChar;
        	public byte XoffChar;
        	public byte ErrorChar;
        	public byte EofChar;
        	public byte EvtChar;
        	public int wReserved1;
        }

        public struct COMMCONFIG
        {
            public long dwSize;
        	public int wVersion;
        	public int wReserved;
        	public DCB dcbx;
        	public long dwProviderSubType;
        	public long dwProviderOffset;
        	public long dwProviderSize;
        	public byte wcProviderData;
        }

        
        [DllImport("kernel32.dll")]
        public static extern bool CommConfigDialog(string lpszName, IntPtr hWnd,
            ref COMMCONFIG lpCC);
        
        private static Configuration instance = new Configuration();
        
        public static Configuration Instance {
            get {
                return instance;
            }
        }
        
        private Configuration()
        {
             _mainicon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
        }
        
        private static string _selmodem_port = null;
        private static Icon _mainicon = null;
        private static COMMCONFIG _commconf;
        private static bool _commconf_iscfg = false;
        
        public static string SelectedModem {
            get {
                return _selmodem_port;
            }
        }
        
        public static Icon MainIcon {
            get {
                return _mainicon;
            }
        }
        
        public static COMMCONFIG CommConfig {
            get {
                return _commconf;
            }
        }

        public static bool CommConfigConfigured {
            get {
                return _commconf_iscfg;
            }
        }
        
        public static bool SelectModem(IWin32Window owner)
        {
            fmSelModem sm = new fmSelModem();
            if (sm.ShowDialog(owner) == DialogResult.OK)
            {
                _selmodem_port = sm.SelectedModem;
                return true;
            }
            return false;
        }
        
        public static bool SelectPort(IWin32Window owner)
        {
            fmPorts fp = new fmPorts();
            if (fp.ShowDialog(owner) == DialogResult.OK)
            {
                _selmodem_port = fp.SelectedPort;
                return true;
            }
            return false;
        }
        
        public static bool ConfigPort(IWin32Window owner)
        {
            if (_selmodem_port != null && _selmodem_port.Length > 0){
                COMMCONFIG ccfg = new COMMCONFIG();
                ccfg.dwSize = Marshal.SizeOf(ccfg);
                if (CommConfigDialog(_selmodem_port, owner.Handle, ref ccfg))
                {
                    _commconf = ccfg;
                    _commconf_iscfg = true;
                    return true;
                }
            }
            return false;
        }
        
        
    }
}
